package application.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import api.Program;
import api.error.ConnectionUnavailableException;
import api.error.InproperProgramException;
import application.FileUtil;
import application.Launcher;

public class ExecuteProgramAction extends AbstractAction{
	
	
	public void actionPerformed(ActionEvent e) {
		File init = null;
		try{
			init = FileUtil.getCodebaseDirectory(Launcher.class).getParentFile();
		}catch(Exception err){}
		JFileChooser choose = null;
		if(init != null)
			choose = new JFileChooser(init);
		else
			choose = new JFileChooser();
		FileFilter f = new FileFilter(){
		
		public boolean accept(File pathname) {
			return pathname.isDirectory() || pathname.getName().endsWith(".rcml");
		}
		
			public String getDescription() {
				return ".xml";
			}
		};
		choose.setFileFilter(f);
		
		int ret = choose.showOpenDialog(null);
		if(ret == JFileChooser.CANCEL_OPTION)
			return;
		
		File file = choose.getSelectedFile();
		
		Launcher.getMainFrame().getMainPanel().executeProgram(file);
		
	}
}
